package RecursiveSearcher;

import java.util.Random;

/**
 * @ClassName LC98
 * @Description 验证是否为二叉搜索树
 * @Author ZJX
 * @Date 2024/9/13 14:19
 * @Version 1.0
 */
public class LC98 {
    private TreeNode prev = null;  // 用来记录前一个节点
    public boolean isValidBST(TreeNode root) {
//        二叉搜索树的中序遍历是一个有序的序列
        return inOrder(root);
    }

    private boolean inOrder(TreeNode root) {
        if (root == null){
            return true;
        }

        if (!inOrder(root.left)){
            return false;
        }

        if ( prev != null && prev.val >= root.val ){
            return false;
        }
        prev = root;

        return inOrder(root.right);
    }
}
